package com.cltech.service.sys.impl;

import com.cltech.dto.SysOrganizationDto;
import com.cltech.entity.sys.SysOrganization;
import com.cltech.entity.sys.SysUser;
import com.cltech.frame.base.impl.BaseServiceImpl;
import com.cltech.frame.result.ResponseBean;
import com.cltech.dto.TreeNode;
import com.cltech.frame.specification.utils.SpecificationSortUtil;
import com.cltech.mapper.SysOrganizationMapper;
import com.cltech.repository.sys.SysOrganizationRepository;
import com.cltech.service.sys.SysOrganizationService;
import com.cltech.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2019/4/6.
 */
@Service("sysOrganizationService")
public class SysOrganizationServiceImpl extends BaseServiceImpl<SysOrganization, String> implements SysOrganizationService {
    private SysOrganizationRepository sysOrganizationRepository;
    @Autowired
    private SysOrganizationMapper sysOrganizationMapper;

    @Autowired
    public void setSysUserRepository(SysOrganizationRepository sysOrganizationRepository) {
        this.sysOrganizationRepository = sysOrganizationRepository;
        super.setBaseRepository(sysOrganizationRepository);
    }

    @Override
    public Page<SysOrganizationDto> findAll(Map<String, String[]> queryParameters, int pageNo, int pageSize) {
        Map<String, Object> map = SpecificationSortUtil.creat(queryParameters, SysOrganization.class);
        Specification<SysOrganization> specification = (Specification<SysOrganization>) map.get(SpecificationSortUtil.SEARCHS);
        Sort sort = (Sort) map.get(SpecificationSortUtil.ORDERS);
        Pageable page = null == sort ? new PageRequest(0, 15) : new PageRequest(0, 15, sort);
        if (null == specification) {
            return sysOrganizationMapper.entityToDto(sysOrganizationRepository.findAll(page));
        } else {
            return sysOrganizationMapper.entityToDto(sysOrganizationRepository.findAll(specification,
                    page));
        }
    }

    @Override
    public ResponseBean<List<TreeNode>> organizationTree(Map<String, String[]> queryParameters) {
        Map<String, Object> map = SpecificationSortUtil.creat(queryParameters, SysOrganization.class);
        Specification<SysOrganization> specification = (Specification<SysOrganization>) map.get(SpecificationSortUtil.SEARCHS);
        List<SysOrganization> sysOrganizations ;
        if (null == specification) {
            sysOrganizations = sysOrganizationRepository.findAll();
        } else {
            sysOrganizations = sysOrganizationRepository.findAll(specification);
        }
        List treeNodes = TreeUtil.buildTree(sysOrganizationMapper.entityToTreeNode(sysOrganizations));
        return ResponseBean.success(treeNodes);
    }

    @Override
    public ResponseBean<SysOrganization> addOrUpdate(SysOrganizationDto dto) {
        SysOrganization entity = sysOrganizationMapper.dtoToEntity(dto);
        if (!StringUtils.isEmpty(dto.getParentId())) {
            SysOrganization parent = findById(dto.getParentId()).getData().orElse(null);
            if (null != parent) {
                entity.setParent(parent);
                entity.setParentIds(StringUtils.isEmpty(parent.getParentIds())
                        ? parent.getId() : parent.getParentIds() + "," + parent.getId());
            }
        }
        return save(entity);
    }
}
